﻿using System;
using System.Collections.Generic;
using System.Data;
using l1cs.server.datatables;
using l1cs.server.model.Instance;
using MySql.Data.MySqlClient;

namespace l1cs.server.model
{
    public class L1Quest
    {
        private static Logger _log = Logger.getLogger( typeof( L1Quest ).FullName );

        public const int QUEST_LEVEL15 = 1;
        public const int QUEST_LEVEL30 = 2;
        public const int QUEST_LEVEL45 = 3;
        public const int QUEST_LEVEL50 = 4;

        public const int QUEST_LYRA = 10;
        public const int QUEST_OILSKINMANT = 11;

        public const int QUEST_DOROMOND = 20;
        public const int QUEST_RUBA = 21;
        public const int QUEST_AREX = 22;

        public const int QUEST_LUKEIN1 = 23;
        public const int QUEST_TBOX1 = 24;
        public const int QUEST_TBOX2 = 25;
        public const int QUEST_TBOX3 = 26;
        public const int QUEST_SIMIZZ = 27;
        public const int QUEST_DOIL = 28;
        public const int QUEST_RUDIAN = 29;
        public const int QUEST_RESTA = 30;
        public const int QUEST_CADMUS = 31;
        public const int QUEST_KAMYLA = 32;
        public const int QUEST_CRYSTAL = 33;
        public const int QUEST_LIZARD = 34;
        public const int QUEST_KEPLISHA = 35;
        public const int QUEST_DESIRE = 36;
        public const int QUEST_SHADOWS = 37;
        public const int QUEST_ROI = 38;
        public const int QUEST_TOSCROLL = 39;
        public const int QUEST_MOONOFLONGBOW = 40;
        public const int QUEST_GENERALHAMELOFRESENTMENT = 41;

        public const int QUEST_END = 255; // 終了済みクエストのステップ

        private L1PcInstance _owner = null;
        private Dictionary<int, int> _quest = null;

        public L1Quest( L1PcInstance owner )
        {
            _owner = owner;
        }

        public L1PcInstance get_owner()
        {
            return _owner;
        }

        public int get_step( int quest_id )
        {
            if ( _quest == null )
            {
                _quest = new Dictionary<int, int>();
                String sqlstr = "SELECT * FROM character_quests WHERE char_id=?1";
                using ( MySqlWrapper wrapper = new MySqlWrapper() )
                {
                    MySqlCommand cmd = new MySqlCommand( sqlstr );
                    cmd.Parameters.Add( "?1", MySqlDbType.Int32 ).Value = quest_id;
                    foreach ( DataRow item in wrapper.GetDataTable( cmd ).Rows )
                    {
                        _quest[(int)item.ItemArray[1]] = (int)item.ItemArray[3];
                    }
                }
            }
            int step;
            _quest.TryGetValue( quest_id, out step );
            return step;
        }

        public void set_step( int quest_id, int step )
        {
            if ( _quest.ContainsKey( quest_id ) != true )
            {

                String sqlstr = @"INSERT INTO character_quests SET
                                    char_id    = ?1, 
                                    quest_id   = ?2, 
                                    quest_step = ?3";
                using ( MySqlWrapper wrapper = new MySqlWrapper() )
                {
                    MySqlCommand cmd = new MySqlCommand( sqlstr );
                    cmd.Parameters.Add( "?1", MySqlDbType.Int32 ).Value = _owner.getId();
                    cmd.Parameters.Add( "?2", MySqlDbType.Int32 ).Value = quest_id;
                    cmd.Parameters.Add( "?3", MySqlDbType.Int32 ).Value = step;
                    try
                    {
                        wrapper.ExecNonQuery( cmd );
                    }
                    catch ( Exception e )
                    {
                        _log.log( Level.SEVERE, e.StackTrace );
                    }
                }
            }
            else
            {
                String sqlstr = @"UPDATE character_quests SET 
                                    quest_step = ?1 
                                    WHERE char_id = ?2 AND quest_id = ?3";
                using ( MySqlWrapper wrapper = new MySqlWrapper() )
                {
                    MySqlCommand cmd = new MySqlCommand( sqlstr );
                    cmd.Parameters.Add( "?1", MySqlDbType.Int32 ).Value = step;
                    cmd.Parameters.Add( "?2", MySqlDbType.Int32 ).Value = _owner.getId();
                    cmd.Parameters.Add( "?3", MySqlDbType.Int32 ).Value = quest_id;
                    wrapper.ExecNonQuery( cmd );
                }
            }

            _quest[quest_id] = step;
        }

        public void add_step( int quest_id, int add )
        {
            int step = get_step( quest_id );
            step += add;
            set_step( quest_id, step );
        }

        public void set_end( int quest_id )
        {
            set_step( quest_id, QUEST_END );
        }

        public bool isEnd( int quest_id )
        {
            if ( get_step( quest_id ) == QUEST_END )
            {
                return true;
            }
            return false;
        }
    }
}
